package com.frxuexi.park2212.visit

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.frxuexi.lib_base.base.BaseActivity
import com.frxuexi.lib_base.entity.VisitStatusBean
import com.frxuexi.park2212.R
import com.frxuexi.park2212.databinding.ActivityVisitBinding
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/app/VisitActivity")
@AndroidEntryPoint
class VisitActivity : BaseActivity<ActivityVisitBinding,VisitViewModel>() {
    val visitStatusAdapter by lazy { VisitStatusAdapter() }
    val visitAdapter by lazy { VisitAdapter() }
    override fun initData() {
        vm.getVisits.observe(this){
            Log.i("===",it.toString())
            visitAdapter.data.clear()
            visitAdapter.apply {
                addData(it)
                notifyDataSetChanged()
            }
        }

        vm.deleteVisit.observe(this){

        }
    }

    override fun initView() {

        binding.headVisit.apply {
            headerBack.setOnClickListener {
                finish()
            }
            headerTitle.setText("访客管理")
            headerAdd.setOnClickListener {
                ARouter.getInstance().build("/app/VisitAddActivity").navigation()
            }
        }

        val list = listOf<VisitStatusBean>(VisitStatusBean("全部状态",true),
            VisitStatusBean("待审核",false),
            VisitStatusBean("已通过",false),
            VisitStatusBean("驳回",false),
            VisitStatusBean("已完成",false))
        visitStatusAdapter.addData(list)
        binding.visitRvstatus.apply {
            adapter=visitStatusAdapter
            layoutManager=LinearLayoutManager(this@VisitActivity,RecyclerView.HORIZONTAL,false)
        }

        visitStatusAdapter.setOnItemClickListener { adapter, view, position ->
            visitStatusAdapter.data.forEach {
                it.isCheck=false
            }
            visitStatusAdapter.data.get(position).isCheck=true
            visitStatusAdapter.notifyDataSetChanged()
            if(position==0){
                vm.getVisits()
            }else{
                vm.getVisitByStatus(visitStatusAdapter.data.get(position).status)
            }
        }

        binding.rvVisit.apply {
            adapter=visitAdapter
            layoutManager=LinearLayoutManager(this@VisitActivity)
        }

        visitAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/VisitApproveActivity")
                .withString("vid",visitAdapter.data.get(position).id.toString()).navigation()
        }

        visitAdapter.setOnItemLongClickListener { adapter, view, position ->
            vm.deleteVisit(visitAdapter.data.get(position).id!!)
            visitAdapter.data.removeAt(position)
            visitAdapter.notifyItemRemoved(position)
            return@setOnItemLongClickListener true
        }

    }

    override fun onResume() {
        super.onResume()
        vm.getVisits()
    }

}